﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using THANHTUNGO.Portal.Components.Security;
using THANHTUNGO.Portal.Components.UI;
using THANHTUNGO.Portal.Modules.NEWS.Components;

namespace THANHTUNGO.Portal.Modules.NEWS.Pages
{
    public partial class ItemEdit : AddEditItemPage
    {
        private void buildCategory()
        {
            DataSet ds = Category.SelectAll(this.ModuleID);
            DataRow[] rootRows = ds.Tables[0].Select("ParentID = 0");

            foreach (DataRow row in rootRows)
            {
                cbCategory.Items.Add(new ListItem(row["Name"].ToString(), row["ID"].ToString()));
                DataRow[] childRows = ds.Tables[0].Select("ParentID = " + row["ID"]);
                if (childRows.Length > 0)
                {
                    foreach (DataRow childRow in childRows)
                    {
                        cbCategory.Items.Add(new ListItem("» " + childRow["Name"], childRow["ID"].ToString()));
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCreationTime.Text = DateTime.Today.ToShortDateString();
                lblCreatedBy.Text = PortalSecurity.CurrentUser.Identity.Email;

                this.buildCategory();

                cbFlow.DataSource = Flow.SelectAll(this.ModuleID);
                cbFlow.DataBind();

                if (this.ItemID > 0)
                {
                    Item item = Item.Load(this.ItemID);
                    if (item != null)
                    {
                        // Lĩnh vực.
                        cbCategory.SelectedValue = item.CategoryID.ToString();

                        // Luồng bài viết.
                        cbFlow.SelectedValue = item.FlowID.ToString();

                        // Tiêu đề.
                        txtTitle.Text = item.Title;

                        // Phụ đề.
                        txtSubTitle.Text = item.SubTitle;
                        // Hình minh họa.
                        txtUrl.Text = item.ImageUrl;

                        // Từ khóa.
                        txtKeywords.Text = item.Keywords;

                        // Tác giả.
                        txtAuthors.Text = item.Authors;

                        // Nội dung.
                        txtContent.Text = Server.HtmlDecode(item.Content);

                        // Đánh giá.
                        cbRates.SelectedValue = item.Rate.ToString();

                        // Lượt xem.
                        txtTotalView.Text = item.TotalViews.ToString("N0");
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    int categoryID = cbCategory.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbCategory.SelectedValue);
                    int flowID = cbFlow.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbFlow.SelectedValue);

                    Item item = new Item();
                    item.CategoryID = categoryID;
                    item.FlowID = flowID;
                    item.Title = txtTitle.Text;
                    item.SubTitle = txtSubTitle.Text;

                    // Hình minh họa
                    item.ImageUrl = txtUrl.Text;

                    item.Keywords = txtKeywords.Text;
                    item.Authors = txtAuthors.Text;
                    item.Content = Server.HtmlEncode(txtContent.Text);

                    item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                    item.CreationTime = DateTime.Now;
                    item.PublishedTime = DateTime.Now;
                    item.LastModificationTime = DateTime.Now;
                    item.Status = cbStatus.SelectedValue;
                    item.Rate = Convert.ToByte(cbRates.SelectedValue);
                    item.TotalViews = Int32.Parse(txtTotalView.Text);

                    if (this.ItemID == 0)
                    {
                        item.CreationTime = DateTime.Now;
                        item.Insert();
                    }
                    else
                    {
                        item.CreationTime = DateTime.Parse(lblCreationTime.Text);                        
                        item.ID = Convert.ToInt32(this.ItemID);
                        item.Update();
                    }

                    this.ReturnToReferrerPage();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Item.Delete(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }
    }
}